package com.jieshuibao.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jieshuibao.model.db.UserDealRecordExample;
import com.jieshuibao.model.db.UserDealRecordExample.Criteria;
import com.jieshuibao.model.domain.BirdsReqeust;
import com.jieshuibao.model.domain.BirdsResponse;
import com.jieshuibao.model.domain.CookieUser;
import com.jieshuibao.service.RedisService;
import com.jieshuibao.service.UserDealRecordService;
import com.jieshuibao.util.PayTypeUtil;

@Controller
@RequestMapping(value = "/birds")
public class BirdsController {
	@Resource
	private RedisService redisService;
	@Resource
	private UserDealRecordService userDealRecordService;

		@RequestMapping(value = "/revelpes", method = RequestMethod.POST)
		@ResponseBody
		public Object queryAllArticle(HttpServletRequest request,
				@RequestBody BirdsReqeust birdsReqeust) {
			BirdsResponse birdsResponse = new BirdsResponse();
			try {
				String cookieId = request.getHeader("cookieId");
				if (cookieId != null) {
					CookieUser cookieUser = redisService.getObejct(cookieId,
							CookieUser.class);
					if (cookieUser != null) {
						String phoneNumber = cookieUser.getPhoneNumber();
						if (phoneNumber != null) {
							String dateTime = birdsReqeust.getDateTime();
							if(dateTime!=null&&!dateTime.equals("")){
								UserDealRecordExample example1 = new UserDealRecordExample();
								UserDealRecordExample example2 = new UserDealRecordExample();
								UserDealRecordExample example3 = new UserDealRecordExample();
								Criteria criteria = example1.createCriteria();
								Criteria criteria2 = example2.createCriteria();
								Criteria criteria3 = example3.createCriteria();
								try {
									java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
											"yyyy-MM-dd HH:mm:ss");
									java.util.Date utilDate = sdf.parse(dateTime);
									criteria.andUserIdEqualTo(phoneNumber);
									criteria.andTradeTypeEqualTo(PayTypeUtil.TRDETYPE_INCOME);
									criteria.andCreateDateGreaterThan(utilDate);
									criteria2.andUserIdEqualTo(phoneNumber);
									criteria2.andTradeTypeEqualTo(PayTypeUtil.TRDETYPE_INCOME);
									criteria2.andCreateDateGreaterThan(utilDate);
									criteria3.andUserIdEqualTo(phoneNumber);
									criteria3.andTradeTypeEqualTo(PayTypeUtil.TRDETYPE_INCOME);
									criteria3.andCreateDateGreaterThan(utilDate);
									
								} catch (Exception e) {
									birdsResponse.setCode("3");
									birdsResponse.setDes("数据格式错误");
								}
								
									criteria.andGoodsTypeEqualTo(PayTypeUtil.GOODSTYPE_SECRETLY);
									Double secretlyMoney = userDealRecordService.getUserdealRecordsByExample(example1);
									if(secretlyMoney==null){
										secretlyMoney=0.0;
									}
									birdsResponse.setSecretlyMoney(secretlyMoney);
									
									criteria2.andGoodsTypeEqualTo(PayTypeUtil.GOODSTYPE_REWARD);
									Double rewardMoney= userDealRecordService.getUserdealRecordsByExample(example2);
									if(rewardMoney==null){
										rewardMoney=0.0;
									}
									birdsResponse.setRewardMoney(rewardMoney);
									
									criteria3.andGoodsTypeEqualTo(PayTypeUtil.GOODSTYPE_ENVELOPES);
									Double envelopesMoney= userDealRecordService.getUserdealRecordsByExample(example3);
									if(envelopesMoney==null){
										envelopesMoney=0.0;
									}
									birdsResponse.setEnvelopesMoney(envelopesMoney);
									birdsResponse.setMoney(secretlyMoney+rewardMoney+envelopesMoney);
									
									birdsResponse.setCode("1");
									birdsResponse.setDes("解税鸟来了");
									
							}else {
									birdsResponse.setCode("3");
									birdsResponse.setDes("数据格式错误");
							}
							
						} else {
							// 重新登录
							birdsResponse.setCode("2");
							birdsResponse.setDes("重新登录");
						}
					} else {
						// 重新登录
						birdsResponse.setCode("2");
						birdsResponse.setDes("重新登录");
					}
				} else {
					// 重新登录
					birdsResponse.setCode("2");
					birdsResponse.setDes("重新登录");
				}
			} catch (Exception e) {
				e.printStackTrace();
				birdsResponse.setCode("0");
				birdsResponse.setDes("系统繁忙");
			}
			return birdsResponse;
		}
}
